package com.cyx.log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Aspect
@Component
public class LogUtils {

    @Pointcut(value ="execution(* com.cyx.service.UserServiceImpl.*(..))" )
    public void myPoint(){}

    // execution(访问权限(可以省略，但不能为*) 返回值类型 方法签名)
    //@Before(value = "execution(* com.cyx.service.UserServiceImpl.*(..))")
    @Before("myPoint()")
    public static void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String funcName = joinPoint.getSignature().getName();
        System.out.println(funcName+"("+Arrays.asList(args)+") 开始执行...");
    }
    @AfterReturning(value = "execution(public * com.cyx.service.UserServiceImpl.*(..))",returning = "result")
    public static void logSuccessReturn(JoinPoint joinPoint,Object result ){
        Object[] args = joinPoint.getArgs();
        String funcName = joinPoint.getSignature().getName();
        System.out.println(funcName+" 成功返回: "+result);
    }
    @AfterThrowing(value = "execution(public * com.cyx.service.UserServiceImpl.*(..))",throwing = "e")
    public static void logExceptionReturn(JoinPoint joinPoint,Exception e){
        Object[] args = joinPoint.getArgs();
        String funcName = joinPoint.getSignature().getName();
        System.out.println(funcName+" 异常返回:"+e.getMessage());
    }
//    @After(value = "execution(* com.cyx.service.UserServiceImpl.*(..))")
    @After("myPoint()")
    public static void logFinal(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String funcName = joinPoint.getSignature().getName();
        System.out.println(funcName+" 最终结束");
    }

    @Around("myPoint()")
    public static Object logAll(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = null;
        Object[] args = joinPoint.getArgs();
        try {
            System.out.println("环绕通知-开始...");
            //利用反射方法调用
            result = joinPoint.proceed(args);
            System.out.println("环绕通知-正常结束");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知-异常");
        } finally {
            System.out.println("环绕通知-最终结束");
        }
        return result;
    }
//    public static void logStart(){
//        System.out.println(method.getName()+"("+ Arrays.asList(args)+") 开始执行...");
//    }
//    public static void logSuccessReturn(Method method, Object[] args, @Nullable Object target){
//        System.out.println(method.getName()+" 成功返回");
//    }
//    public static void logExceptionReturn(Method method, Object[] args, @Nullable Object target){
//        System.out.println(method.getName()+" 异常返回");
//    }
//
//    public static void logFinal(Method method, Object[] args, @Nullable Object target){
//        System.out.println(method.getName()+"("+ Arrays.asList(args)+") 最终结束");
//    }
}
